(function ($) {
    $.zJSON = function (_optons) {
        var options = {};
        var s = $.trim(_optons);
        if (s) {
            if (s.substring(0, 1) != '{') {
                s = '{' + s + '}';
            }
            options = (new Function('return ' + s))();
            return options;
        }
    }
})(jQuery)	;

function common_save(_$) {
		var target = $(_$);
		var options = $.zJSON(target.attr('dialog-options'));
		var className = target.attr('class');
		var url = options.url;
		var style=options.style;
		var tab = target.parents('.tabpanel');
		var datagrid = tab.find('.easyui-datagrid');
		var item;
		if(className.indexOf('editbtn')>0){ //表明是編輯按鈕
			 item =datagrid.datagrid('getSelected');
				if (item == null || item == undefined) {
					$.messager.alert('信息提示', '请选择你要修改的信息！', 'info');
					return;
				}
			}
		var dialog = $('<div></div>');
		dialog.attr('class','easyui-dialog').attr('style',style);
		tab.append(dialog);
		dialog.dialog({
			closed : false,
			modal : true,
			title : "添加信息",
			href:url+"?id="+(item==null?0:item.id)+"&rand=" + new Date().getTime(),
			buttons : [ {
				text : '确定',
				iconCls : 'icon-ok',
				handler : function() {
					var form = dialog.find('form');
					form.form('submit', {
						url : form.attr('url'),
						success : function(data) {
							if (data) {
								$.messager.alert('信息提示', '提交成功！', 'info');
								dialog.dialog('close');
                                dialog.remove();
								common_refresh(_$);
							} else {
								$.messager.alert('信息提示', '提交失败！', 'info');
							}
						}
					});
				}
			}, {
				text : '取消',
				iconCls : 'icon-cancel',
				handler : function() {
					dialog.dialog('close');
                    dialog.remove();
				}
			} ]
		});
	}

 function common_del(_$) {
	 var target = $(_$);
		var options = $.zJSON(target.attr('dialog-options'));
		var url = options.url;
		var tab = target.parents('.tabpanel');
		var datagrid = tab.find('.easyui-datagrid');
		
		
	 var items = datagrid.datagrid('getSelections');
		var ids = new Array();
		$(items).each(function() {
			ids.push(this.id);
		});
		if (ids.length <= 0) {
			$.messager.alert('信息提示', '请选择要删除的选项', 'info');
			return;
		}
		$.messager.confirm('信息提示', '确定要删除该记录？', function(result) {
			if (result) {
				//alert(ids);return;
				$.ajax({
					url : url,
					dataType : 'json',
					data : {
						'ids' : ids
					},
					traditional : true,
					success : function(data) {
						if (data) {
							$.messager.alert('信息提示', '删除成功！', 'info');
							common_refresh(_$);
						} else {
							$.messager.alert('信息提示', '删除失败！', 'info');
						}
					}
				});
			}
		});
 }	
 
 function common_refresh(_$){
	 var target = $(_$);
		var tab = target.parents('.tabpanel');
		var datagrid = tab.find('.easyui-datagrid');
		var form = tab.find('form');
		datagrid.datagrid({
			queryParams : form.serializeObject()
		});
 }
 
	
	